package com.zxk28.sqlbuilders;

public class SQLUpdateBuilder {
	private String prefix = "UPDATE ";
	private String tableClause = null;
	private String setClause = null;
	private String whereClause = null;

	public SQLUpdateBuilder setUpdateTable(String talbe) {
		this.tableClause = talbe;

		return this;
	}

	public SQLUpdateBuilder setSetClause(Object... columnsAndValues) {
		this.setClause = " SET ";

		for (int i = 0; i < columnsAndValues.length; i += 2) {
			this.setClause += columnsAndValues[i].toString();
			this.setClause += "=";
			if (columnsAndValues[i + 1] instanceof String)
				this.setClause += "'" + columnsAndValues[i + 1].toString()
						+ "'";
			else
				this.setClause += columnsAndValues[i + 1].toString();

			if (i < columnsAndValues.length - 2)
				this.setClause += ", ";
		}
		return this;
	}

	public SQLUpdateBuilder setWhereClause(String where) {
		this.whereClause = " WHERE " + where;

		return this;
	}

	public String getCmd() {
		if (tableClause == null)
			return null;
		if (setClause == null)
			return null;

		String cmd = prefix + tableClause + setClause;

		if (whereClause != null)
			cmd += whereClause;
		cmd += ";";

		return cmd;
	}

	@Override
	public String toString() {
		return getCmd();
	}
}
